package biz

import (
	"fmt"
	"log"
	"server/base"
	"server/base/db"
	"strconv"
	"strings"
)

func SysvTabString() string {
	out := `[
	{'name':'5.05', 'id':1, 'added':false},
    {'name':'6.72', 'id':2, 'added':false},
    {'name':'7.02', 'id':3, 'added':false},
    {'name':'7.55', 'id':4, 'added':false},
    {'name':'9.00', 'id':5, 'added':false},
	]`
	return out
}

type SysvRow struct {
	Id    int    `json:"id"`
	Name  string `json:"name"`
	Added bool   `json:"added"`
}

func SysvTab(p string) []*SysvRow {
	var o = []*SysvRow{}
	err := ParseSetting(p, "sysv", &o)
	if err != nil {
		log.Println("LoadSysv err:", err)
	}
	return o
}

func GenTabString() string {
	out := `[
    {'id':0, 'name':'所有'},
    {'id':1, 'name':'角色扮演'},
    {'id':2, 'name':'冒险'},
    {'id':3, 'name':'解谜'},
    {'id':4, 'name':'动作'},
    {'id':5, 'name':'格斗'},
    {'id':6, 'name':'体育'},
    {'id':7, 'name':'休闲'},
    {'id':8, 'name':'射击'},
    {'id':9, 'name':'音乐'},
]`
	return out
}

type GenRow struct {
	Id   int    `json:"id"`
	Name string `json:"name"`
}

func GenTab(p string) []*GenRow {
	var o = []*GenRow{
		{-1, "最新游戏"},
		{-2, "最热游戏"},
		{-3, "双人游戏"},
		{-4, "聚会游戏"},
		{-5, "VR游戏"},
	}
	var o2 []*GenRow
	err := ParseSetting(p, "genre", &o2)
	if err != nil {
		log.Println("LoadGenres err:", err)
	}
	o = append(o, o2...)
	return o
}

type HDDRow struct {
	Cap  float64 `json:"cap"`
	Size float64 `json:"size"`
	Name string  `json:"name"`
}

func HddTab(p string) []*HDDRow {
	var out = []*HDDRow{}
	rows := db.Replica.Queryx(`select cap, size, name from hdd where platform=? and status=1 order by cap`,
		p)
	defer rows.Close()
	for rows.Next() {
		var it = &HDDRow{}
		rows.Scan(&it.Cap, &it.Size, &it.Name)
		out = append(out, it)
	}
	return out
}

type GameRow struct {
	Id         int     `json:"id"`
	Name       string  `json:"name"`
	En         string  `json:"en"`
	Aka        string  `json:"aka"`
	Photo      string  `json:"photo"`
	Genres     []int   `json:"genres"`
	Des        string  `json:"des"`
	Size       float64 `json:"size"`
	Pinyin     string  `json:"pinyin"`
	Letter     string  `json:"letter"`
	ReleasedAt int     `json:"released_at"`
	Players    int     `json:"players"`
	Vr         bool    `json:"vr"`
	Adds       int     `json:"adds"`
	Tags       string  `json:"tags"`
	Added      bool    `json:"added"`
}

type VersionRow struct {
	Id     int    `json:"id"`
	GameId int    `json:"game_id"`
	Name   string `json:"name"`
	Des    string `json:"des"`
	Photo  string `json:"photo"`
}

type PkgRow struct {
	Id        int     `json:"id"`
	VersionId int     `json:"version_id"`
	GameId    int     `json:"game_id"`
	Name      string  `json:"name"`
	Size      float64 `json:"size"`
	Type      int     `json:"type"`
	Sysv      []int   `json:"sysv"`
	Ver       float64 `json:"ver"`
	Lang      string  `json:"lang"`
	BindId    int     `json:"bind_id"`
	Added     bool    `json:"added"`
	Pending   bool    `json:"pending"`
}

func ToPhotoURL(path string) string {
	if path == "" {
		return ""
	}
	return "/uploads/" + path
}

func ToIcon(en, path string) string {
	if path != "" {
		return ToPhotoURL(path)
	}
	return fmt.Sprintf("/uploads/save/%s/ICON_S.png", en)
}

func GameTab(platform string) []*GameRow {
	var out = []*GameRow{}
	var rows *db.MustXRows
	if platform == "" {
		rows = db.Replica.Queryx(`select id,name,en,aka,photo,genre,des,released_at,players,vr,adds,tags from game where visible=1`)
	} else {
		rows = db.Replica.Queryx(`select id,name,en,aka,photo,genre,des,released_at,players,vr,adds,tags from game where visible=1 and platform=?`, platform)
	}

	defer rows.Close()
	for rows.Next() {
		var r = &GameRow{}
		var genre string
		rows.Scan(&r.Id, &r.Name, &r.En, &r.Aka, &r.Photo, &genre, &r.Des, &r.ReleasedAt, &r.Players, &r.Vr, &r.Adds, &r.Tags)
		r.Genres = base.ToInts(genre)
		r.Photo = ToIcon(r.En, r.Photo)
		py := base.PinyinString(r.Name)
		letter := ""
		if len(py) > 0 {
			letter = strings.ToLower(py[:1])
		}
		r.Pinyin = py
		r.Letter = letter
		out = append(out, r)
	}
	return out
}

func VersionTab(platform string) []*VersionRow {
	var out = []*VersionRow{}
	var rows *db.MustXRows
	if platform == "" {
		rows = db.Replica.Queryx(`select id,game_id,name,des,photo from version where visible=1`)
	} else {
		rows = db.Replica.Queryx(`select v.id,v.game_id,v.name,v.des,v.photo from version v
inner join game g on g.id=v.game_id
where v.visible=1 and g.platform=?`, platform)
	}
	defer rows.Close()
	for rows.Next() {
		var r = &VersionRow{}
		rows.Scan(&r.Id, &r.GameId, &r.Name, &r.Des, &r.Photo)
		r.Photo = ToPhotoURL(r.Photo)
		out = append(out, r)
	}
	return out
}

func PkgTab(platform string) []*PkgRow {
	var out = []*PkgRow{}
	var sysv string
	var rows *db.MustXRows
	if platform == "" {
		rows = db.Replica.Queryx(`select id,version_id,game_id,name,sizea,type,sysv,ver,lang,bind_id from package 
where visible=1`)
	} else {
		rows = db.Replica.Queryx(`select p.id,p.version_id,p.game_id,p.name,p.sizea,p.type,p.sysv,p.ver,p.lang,
p.bind_id from package p
inner join game g on g.id=p.game_id
where p.visible=1 and g.platform=?`, platform)
	}
	defer rows.Close()
	for rows.Next() {
		var r = &PkgRow{}
		rows.Scan(&r.Id, &r.VersionId, &r.GameId, &r.Name, &r.Size, &r.Type, &sysv, &r.Ver, &r.Lang, &r.BindId)
		ss := strings.Split(sysv, ",")
		for _, s := range ss {
			v, _ := strconv.Atoi(s)
			r.Sysv = append(r.Sysv, v)
		}
		out = append(out, r)
	}
	return out
}

func OrderGameTab(order_id int) []*GameRow {
	var out = []*GameRow{}
	if order_id == 0 {
		return out
	}
	var rows *db.MustXRows
	rows = db.Replica.Queryx(`select op.game_id, g.name, g.en, g.aka, g.photo, g.genre, g.des, sum(op.pkg_size) size, g.players 
from orders o
inner join order_pkg op on op.order_id=o.id
inner join game g on g.id=op.game_id
where o.id=?
group by op.game_id`, order_id)
	defer rows.Close()
	for rows.Next() {
		var r = &GameRow{}
		var genre string
		rows.Scan(&r.Id, &r.Name, &r.En, &r.Aka, &r.Photo, &genre, &r.Des, &r.Size, &r.Players)
		r.Genres = base.ToInts(genre)
		r.Photo = ToIcon(r.En, r.Photo)
		py := base.PinyinString(r.Name)
		letter := ""
		if len(py) > 0 {
			letter = strings.ToLower(py[:1])
		}
		r.Pinyin = py
		r.Letter = letter
		out = append(out, r)
	}
	return out
}
